#ifndef BANK_SERVER_H
#define BANK_SERVER_H

#define BANK_DB "bank.db"
#define CLIENT_ID "onlyone"
extern BankDatabase g_bankDb;

#define CHECK_UPDATE_COUNT(CLIENT_ARGS)                                 \
  do                                                                    \
    {                                                                   \
    int expectedUpdateCount = CLIENT_ARGS->expectedUpdateCount;         \
    int nextUpdateCount = g_bankDb.GetUpdateCount (CLIENT_ID) + 1;      \
    if (nextUpdateCount != expectedUpdateCount)                         \
      {                                                                 \
      std::string err;                                                  \
      if (nextUpdateCount < expectedUpdateCount)                        \
        {                                                               \
        WRITE_STRING (err, "database is outdated! - ");                 \
        }                                                               \
      WRITE_STRING (err, "UpdateCount expected by the client ("         \
        << expectedUpdateCount << ") does not match with the server ("  \
        << nextUpdateCount << ")!");                                    \
        std::runtime_error e (err);                                     \
      DEBUG_WRN (e.what ());                                            \
      throw e;                                                          \
      }                                                                 \
    } while (false);

template <typename ARGS> 
bool CheckUpdateCount (ARGS* args)
  {
  try 
    {
    CHECK_UPDATE_COUNT (args);
    return 1;
    }
  catch (std::runtime_error e)
    {
    return 0;
    }
  }

///**
// * Check if the UpdateCount expected by the calling client matches the server-side UpdateCount
// *
// * \param args a RPC arguments
// * \return the next UpdateCount expected the server
// */
//template <typename ARGS> 
//int
//CheckUpdateCount (ARGS args)
//  {                                                       
//  int expectedUpdateCount = args->expectedUpdateCount;         
//  int nextUpdateCount = g_bankDb.GetUpdateCount (CLIENT_ID) + 1;      
//  if (nextUpdateCount != expectedUpdateCount)                         
//    {                                                                 
//    std::string err;                                                       
//    WRITE_STRING (err, "UpdateCount expected by the client ("         
//      << expectedUpdateCount << ") does not match with the server ("  
//      << nextUpdateCount << ")!");                                    
//    std::runtime_error e (err);                                            
//    DEBUG_WRN (e.what ());                                            
//    throw e;
//    }
//
//  DEBUG_LOG ("nextUpdateCount at server-side = " << nextUpdateCount);
//  return nextUpdateCount;
//  }

#endif /* BANK_SERVER_H */
